package com.zhoukx.singleton.type2;

/**
 * @ClassName SingletonTest01
 * @Description: 饿汉式 静态代码块
 * @Author zhoukx
 * @Date 2020/8/4
 **/
public class SingletonTest02 {
    public static void main(String[] args) {
        Singleton instance = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println("instance:"+instance.hashCode());
        System.out.println("instance2:"+instance2.hashCode());
    }
}

// 饿汉式（静态变量）
class Singleton{
   private static Singleton instance;
   // 外部不能new 对象
   private Singleton(){

   }
    static {
        instance = new Singleton();
    }

    // 提供一个公有的静态方法，对外提供对象
    public static Singleton getInstance(){
        return  instance;
    }

}
